﻿using System.Threading.Tasks;
using System.Web.Mvc;
using Microsoft.AspNet.SignalR;
using Web.Hubs;
using Web.Infrastructure.Helpers;
using Web.Infrastructure.Interface;
using Web.Infrastructure.Types;
using Web.InputModels;
using Web.Viewmodels;

namespace Web.Controllers
{
    public class BoardController : Controller
    {
        [Route("Board")]
        [HttpGet]
        public async Task<ActionResult> Board(UIEnums.TimeControl timeControl, UIEnums.Color player1Color, int gameId, UIEnums.GameType gameType)
        {
            var vm = new BoardViewModel
            {
                GameId = gameId,
                TimeControl = (int)timeControl,
                Player1Color = player1Color,
                GameType = gameType
            };

            //check ak webservice
            return View("Board", vm);
        }

        [Route("Board/Move")]
        [HttpPost]
        public async Task<ActionResult> Move(FindMoveInputModel inputModel)
        {
            var response = await AIInterface.FindMove(MappingHelper.MapFindMoveInputModel(inputModel));

            var context = GlobalHost.ConnectionManager.GetHubContext<BoardHub>();
            
            context.Clients.Client(inputModel.ConnectionId).move(response.MoveSource, response.MoveTarget);
            return null;
        }
    }
}